﻿using System;
using System.Collections.Generic;
using System.Linq;

using Util;

namespace Manager
{
    /// <summary>
    /// WishManager 的摘要说明
    /// </summary>
    public class WishManager
    {
        public WishManager()
        {
        }

        /// <summary>
        /// 获得指定ID的纸条
        /// </summary>
        /// <param name="wid">纸条ID</param>
        /// <returns>纸条对象</returns>
        public Wish GetWish(int wid)
        {
            try
            {
                WishWallDataContext dataContext = new WishWallDataContext();

                Wish wish = (from w in dataContext.Wish where w.wid == wid select w).Single<Wish>();

                return wish;
            }
            catch(Exception e)
            {
                LogUtil.ErrorException(e);
                return null;
            }
        }

        /// <summary>
        /// 获得指定主题ID的纸条
        /// </summary>
        /// <param name="sid">主题ID</param>
        /// <param name="start">从哪条开始取</param>
        /// <param name="limit">取多少个纸条</param>
        /// <returns>纸条集合</returns>
        public IList<Wish> GetWishes(int sid, int start, int limit)
        {
            try
            {
                WishWallDataContext dataContext = new WishWallDataContext();
                IList<Wish> wishes = (from w in dataContext.Wish where w.subjectId == sid orderby w.wid descending select w).Skip(start).Take(limit).ToList<Wish>();

                return wishes;
            }
            catch (Exception e)
            {
                LogUtil.ErrorException(e);
                return null;
            }
        }

        /// <summary>
        /// 获得某主题纸条总数
        /// </summary>
        /// <param name="sid"></param>
        /// <returns></returns>
        public int GetWishesCount(int sid)
        {
            WishWallDataContext dataContext = new WishWallDataContext();
            int count = (from w in dataContext.Wish where w.subjectId == sid select w).Count();

            return count;
        }

        /// <summary>
        /// 添加纸条
        /// </summary>
        /// <param name="w">纸条对象</param>
        public void Add(Wish w)
        {
            WishWallDataContext dataContext = new WishWallDataContext();
            dataContext.Wish.InsertOnSubmit(w);

            dataContext.SubmitChanges();
        }

        /// <summary>
        /// 获得纸条图片分类
        /// </summary>
        /// <returns>分类对象集合</returns>
        public IList<WishImgCls> GetWishImgClasses()
        {
            WishWallDataContext dataContext = new WishWallDataContext();

            IList<WishImgCls> imgCls = (from cls in dataContext.WishImgCls select cls).ToList<WishImgCls>();

            return imgCls;
        }

        /// <summary>
        /// 获得某分类的所有图片
        /// </summary>
        /// <param name="clsId">分类ID</param>
        /// <returns>图片集合</returns>
        public IList<WishImg> GetWishImgs(int clsId)
        {
            WishWallDataContext dataContext = new WishWallDataContext();

            IList<WishImg> imgCls = (from img in dataContext.WishImg where img.clsId == clsId select img).ToList<WishImg>();

            return imgCls;
        }

        /// <summary>
        /// 获得条纸所有形状
        /// </summary>
        /// <returns>形状集合</returns>
        public IList<WishShape> GetWishShapes()
        {
            WishWallDataContext dataContext = new WishWallDataContext();

            IList<WishShape> shapes = (from s in dataContext.WishShape select s).ToList<WishShape>();

            return shapes;
        }

        /// <summary>
        /// 获得某形状的所有可选颜色值
        /// </summary>
        /// <param name="shape"></param>
        /// <returns></returns>
        public IList<WishColor> GetWishColors(string shape)
        {
            WishWallDataContext dataContext = new WishWallDataContext();

            IList<WishColor> colors = (from c in dataContext.WishColor where c.shapeId == shape select c).ToList<WishColor>();

            return colors;
        }
    }
}